package cn.tj.wenda.cloud.bas.rest.impl;

import java.util.List;

import cn.tj.wenda.cloud.bas.rest.BasUserRest;
import cn.tj.wenda.cloud.exception.BusinessException;
import cn.tj.wenda.cloud.other.rest.BookRest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cn.tj.wenda.cloud.bas.entity.BasLoginInfo;
import cn.tj.wenda.cloud.bas.entity.BasPassword;
import cn.tj.wenda.cloud.bas.service.BasUserService;
import cn.tj.wenda.cloud.entity.ResponseBean;

/**
 * @author Wenda
 */
@RestController
@Scope("prototype")
@Slf4j
public class BasUserRestImpl implements BasUserRest {
	

	@Autowired
	BasUserService basUserService;

	@Autowired
	BookRest bookRest;

	@Override
	public ResponseBean select(int page, int limit, String params) {
		ResponseBean res = new ResponseBean();

		try {
			res = bookRest.getBookList(page,limit,"{}");
			res.setErrMsg("测试循环调用 从bas-user-->other-book-->bas-tenant,最终返回tenant的数据");//负载均衡测试

		}
		catch(BusinessException e) {
			res.setErr("1");
			res.setErr(e.getErrMsg());
		}
		catch(Exception ex) {
			res.setErr("1");
			res.setErr(ex.getLocalizedMessage());
			log.error("获取用户信息时出错",ex);
		}

		return res;
	}

	@Override
	public ResponseBean restPwd(@RequestBody List<String> userIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResponseBean updPwd(@RequestBody BasPassword basPassword) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResponseBean login(@RequestBody BasLoginInfo basLoginInfo) {
		ResponseBean res = new ResponseBean();
        
        try {
        	String ret = basUserService.login(basLoginInfo);
        	res.setData(ret);
        }
        catch(BusinessException e) {
        	res.setErr("1");
        	res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
        	res.setErr("1");
        	res.setErr(ex.getLocalizedMessage());
			log.error("用户【" + basLoginInfo.getAccount() + "】登陆时出错",ex);
        }
        
        return res;
	}

}
